Page({
  data: {
    currentTab: 'all',
    orders: [
      {
        id: 1,
        dishNames: '小炒肉、回锅肉、炝炒莲白',
        dishImages: ['/pages/images/dish1.jpg', '/pages/images/dish2.jpg', '/pages/images/dish3.jpg'],
        totalPrice: '58',
        status: 'pending',
        statusText: '待付款',
        statusClass: 'pending',
        actionType: 'pay',
        actionText: '立即付款',
        actionClass: 'primary'
      },
      {
        id: 2,
        dishNames: '小炒肉、回锅肉、炝炒莲白',
        dishImages: ['/pages/images/dish1.jpg', '/pages/images/dish2.jpg', '/pages/images/dish3.jpg'],
        totalPrice: '58',
        status: 'delivery',
        statusText: '待收取',
        statusClass: 'delivery',
        actionType: 'confirm',
        actionText: '确认收货',
        actionClass: 'secondary'
      },
      {
        id: 3,
        dishNames: '小炒肉、回锅肉、炝炒莲白',
        dishImages: ['/pages/images/dish1.jpg', '/pages/images/dish2.jpg', '/pages/images/dish3.jpg'],
        totalPrice: '58',
        status: 'cancelled',
        statusText: '已取消',
        statusClass: 'cancelled',
        actionType: 'detail',
        actionText: '详情',
        actionClass: 'outline'
      },
      {
        id: 4,
        dishNames: '小炒肉、回锅肉、炝炒莲白',
        dishImages: ['/pages/images/dish1.jpg', '/pages/images/dish2.jpg', '/pages/images/dish3.jpg'],
        totalPrice: '58',
        status: 'completed',
        statusText: '已完成',
        statusClass: 'completed',
        actionType: null,
        actionText: '',
        actionClass: ''
      }
    ],
    filteredOrders: []
  },

  onLoad() {
    this.filterOrders()
  },

  onShow() {
    // 页面显示时刷新订单数据
    this.refreshOrders()
  },

  // 切换标签
  switchTab(e) {
    const tab = e.currentTarget.dataset.tab
    this.setData({
      currentTab: tab
    })
    this.filterOrders()
  },

  // 根据标签筛选订单
  filterOrders() {
    const { currentTab, orders } = this.data
    let filtered = []

    switch (currentTab) {
      case 'all':
        filtered = orders
        break
      case 'pending':
        filtered = orders.filter(order => order.status === 'pending')
        break
      case 'delivery':
        filtered = orders.filter(order => order.status === 'delivery')
        break
      case 'review':
        filtered = orders.filter(order => order.status === 'review')
        break
      default:
        filtered = orders
    }

    this.setData({
      filteredOrders: filtered
    })
  },

  // 刷新订单数据
  refreshOrders() {
    // 这里可以调用API刷新订单数据
    this.filterOrders()
  },

  // 处理订单操作
  handleOrderAction(e) {
    const { order, action } = e.currentTarget.dataset
    const { actionType } = order

    switch (actionType) {
      case 'pay':
        this.handlePayment(order)
        break
      case 'confirm':
        this.confirmReceipt(order)
        break
      case 'detail':
        this.viewOrderDetail(order)
        break
      default:
        break
    }
  },

  // 处理付款
  handlePayment(order) {
    wx.showModal({
      title: '确认付款',
      content: `确认支付 ¥${order.totalPrice} 吗？`,
      success: (res) => {
        if (res.confirm) {
          // 这里应该调用实际的支付接口
          wx.showLoading({
            title: '支付中...'
          })
          
          setTimeout(() => {
            wx.hideLoading()
            wx.showToast({
              title: '支付成功',
              icon: 'success'
            })
            
            // 更新订单状态
            this.updateOrderStatus(order.id, 'delivery')
          }, 2000)
        }
      }
    })
  },

  // 确认收货
  confirmReceipt(order) {
    wx.showModal({
      title: '确认收货',
      content: '确认已收到商品吗？',
      success: (res) => {
        if (res.confirm) {
          wx.showToast({
            title: '收货成功',
            icon: 'success'
          })
          
          // 更新订单状态
          this.updateOrderStatus(order.id, 'completed')
        }
      }
    })
  },

  // 查看订单详情
  viewOrderDetail(order) {
    wx.navigateTo({
      url: `/pages/order-detail/order-detail?id=${order.id}`
    })
  },

  // 更新订单状态
  updateOrderStatus(orderId, newStatus) {
    const { orders } = this.data
    const orderIndex = orders.findIndex(order => order.id === orderId)
    
    if (orderIndex !== -1) {
      const order = orders[orderIndex]
      let statusText = ''
      let statusClass = ''
      let actionType = null
      let actionText = ''
      let actionClass = ''

      switch (newStatus) {
        case 'delivery':
          statusText = '待收取'
          statusClass = 'delivery'
          actionType = 'confirm'
          actionText = '确认收货'
          actionClass = 'secondary'
          break
        case 'completed':
          statusText = '已完成'
          statusClass = 'completed'
          actionType = null
          actionText = ''
          actionClass = ''
          break
        default:
          break
      }

      orders[orderIndex] = {
        ...order,
        status: newStatus,
        statusText,
        statusClass,
        actionType,
        actionText,
        actionClass
      }

      this.setData({ orders })
      this.filterOrders()
    }
  }
}) 